<template>
  <Panel>
    <template v-slot:header> 商品分类 </template>
    <template v-slot:con>
      <el-row>
        <el-button type="primary" size="mini" @click="dialogVisible = true"
          >添加分类</el-button
        >
      </el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column label="分类名称">
          <template v-slot="{ row }">
            <span v-if="row.flag">{{ row.cateName }}</span>
            <el-input v-else v-model="row.cateName" size="small "></el-input>
          </template>
        </el-table-column>
        <el-table-column label="是否禁用">
          <template v-slot="{ row }">
            <el-switch
              :disabled="row.flag"
              :active-value="1"
              :inactive-value="0"
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button
              :type="row.flag ? 'primary' : 'success'"
              size="mini"
              @click="editcate(row)"
              >{{ row.flag ? "编辑" : "完成" }}</el-button
            >
            <el-button type="danger" size="mini" @click="delcate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 4, 6, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 模态框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
        <el-form
          :model="cateForm"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <h2>添加分类</h2>
          <el-input
            v-model="cateForm.cateName"
            placeholder="请输入分类名称"
          ></el-input>
          <h2>是否禁用</h2>
          <el-switch
            v-model="cateForm.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addcate">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </Panel>
</template>

<script>
import { addCate, proCate, delCate, editCate } from "../../api/pro.js";
export default {
  created() {
    this.procate();
  },
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 4,
      total: 0,
      dialogVisible: false,
      cateForm: {
        cateName: "",
        state: 0,
      },
    };
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.procate();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.procate();
    },
    // 获取商品列表
    async procate() {
      const data = await proCate({
        pageSize: this.pageSize,
        currentPage: this.currentPage,
      });
      data.data.forEach((item) => {
        item.flag = true;
      });
      this.total = data.total;
      this.tableData = data.data;
    },
    // 添加分类
    async addcate() {
      const data = await addCate(this.cateForm);
      if (data.code === 0) {
        this.$message({
          message: data.msg,
          type: "success",
        });
        this.dialogVisible = false;
        this.procate();
      }
      this.cateForm = [];
    },
    // 删除商品
    async delcate(id) {
      const data = await delCate({
        id,
      });
      if (data.code === 0) {
        this.$message({
          message: data.msg,
          type: "success",
        });
        this.procate();
      }
    },
    //  修改编辑商品
    async editcate(row) {
      if (row.flag) {
        row.flag = false;
      } else {
        const data = await editCate({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.state),
        });
        // console.log(data);

        if (data.code === 0) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          row.flag = true;
          // console.log(row.state, row);
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
h2 {
  margin: 10px 0;
  font-weight: bold;
}
.el-pagination {
  padding: 10px;
}
/deep/.paner-con {
  padding: 10px 30px;
}
</style>